package com.vikutorika.dao.imp;

import com.vikutorika.dao.BasicDAO;
import com.vikutorika.dao.PersonalTextMessageDAO;
import com.vikutorika.entity.PersonalTextMessage;
import com.vikutorika.util.V_DBUtils;
import com.vikutorika.vo.MessageVo;
import com.vikutorika.vo.SendMessageVo;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.Timestamp;
import java.util.List;

public class PersonalTextMessageDAOImp extends BasicDAO<PersonalTextMessage> implements PersonalTextMessageDAO {

    @Override
    public List<PersonalTextMessage> getAll() {
        Connection conn = null;
        try {
            conn = V_DBUtils.getConnection();
            String sql = "select * from personal_text_message";
            QueryRunner qr = new QueryRunner();
            List<PersonalTextMessage> list = qr.query(conn, sql, new BeanListHandler<>(PersonalTextMessage.class));
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            V_DBUtils.close(null, null, conn);
        }
        return null;
    }

    @Override
    public List<PersonalTextMessage> getUnReadBySenderUID_AND_ReceiverUID(Integer sender_uid, Integer receiver_uid) {
        Connection conn = null;
        try {
            conn = V_DBUtils.getConnection();
            String sql = "select * from personal_text_message where state = '未读' and sender_uid = ? and receiver_uid = ? order by creat_time desc";
            QueryRunner qr = new QueryRunner();
            List<PersonalTextMessage> list = qr.query(conn, sql, new BeanListHandler<>(PersonalTextMessage.class),sender_uid,receiver_uid);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            V_DBUtils.close(null, null, conn);
        }
        return null;
    }

    @Override
    public List<PersonalTextMessage> getReadBySenderUID_AND_ReceiverUID(Integer sender_uid,Integer receiver_uid) {
        Connection conn = null;
        try {
            conn = V_DBUtils.getConnection();
            String sql = "select * from personal_text_message where state = '已读' and sender_uid = ? and receiver_uid = ? or state = '已读' and sender_uid = ? and receiver_uid = ? order by creat_time asc";
            QueryRunner qr = new QueryRunner();
            List<PersonalTextMessage> list = qr.query(conn, sql, new BeanListHandler<>(PersonalTextMessage.class),sender_uid,receiver_uid,receiver_uid,sender_uid);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            V_DBUtils.close(null, null, conn);
        }
        return null;
    }

    @Override
    public void setRead(Integer rid, Integer uid, Timestamp time) {
        Connection conn = null;
        try {
            conn = V_DBUtils.getConnection();
            QueryRunner qr = new QueryRunner();
            String sql ="update personal_text_message set state = '已读' where sender_uid = ? and receiver_uid = ? and creat_time <= ? ";
            qr.update(conn,sql,rid,uid,time);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            V_DBUtils.close(null,null,conn);
        }
    }

    @Override
    public int totalRow() {
        Connection conn = null;
        try {
            conn = V_DBUtils.getConnection();
            String sql = "select count(*) from personal_text_message";
            QueryRunner qr = new QueryRunner();
            Integer count = qr.query(conn, sql, new ScalarHandler<>());
            return count;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            V_DBUtils.close(null, null, conn);
        }
        return 0;
    }

    @Override
    public List<PersonalTextMessage> getBySenderUID_AND_ReceiverUID(Integer sender_uid, Integer receiver_uid,Integer count) {
        Connection conn = null;
        try {
            conn = V_DBUtils.getConnection();
            String sql = "select * from personal_text_message where sender_uid = ? and receiver_uid = ? or sender_uid = ? and receiver_uid = ? order by creat_time desc limit ?";
            QueryRunner qr = new QueryRunner();
            List<PersonalTextMessage> list = qr.query(conn, sql, new BeanListHandler<>(PersonalTextMessage.class), sender_uid,receiver_uid,receiver_uid,sender_uid,count);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            V_DBUtils.close(null, null, conn);
        }
        return null;
    }

    @Override
    public Integer insert(SendMessageVo vo) {
        Connection conn = null;
        try {
            conn = V_DBUtils.getConnection();
            String sql = "insert into personal_text_message value(default,?,?,default,?,default)";
            QueryRunner qr = new QueryRunner();
            Integer insert = qr.update(conn,sql,vo.getUid(),vo.getId(),vo.getContent());
            return insert;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            V_DBUtils.close(null, null, conn);
        }
        return 0;
    }

}
